package ihm.overlays;

import java.awt.event.WindowStateListener;
import java.util.ArrayList;
import java.util.List;

import ihm.main.Window;
import ihm.tabs.PicturesTab;
import ihm.widgets.Picture;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

import tools.ImageConverter;

import com.lo23.data.serializable.PictureCom;

public class AlbumOverlay extends Composite {
	
	private String aID;
	private String albumName;

	public AlbumOverlay( Composite father ) {
		super( father, SWT.NONE );
		setLayout( new GridLayout( 4, true ) );
		aID = "";
		albumName = "";
		
		addButton();
		addLabel( "NO ALBUM" );
	}

	public void addPicture( String pictureName, Image picture, String pID ) {
		new Picture( this, pictureName, picture, pID, aID, albumName );
	}

	public void update( String albumID, String albumName ) {
		this.aID = albumID;
		this.albumName = albumName;
		clearLayout();
		addButton();
		addLabel( albumName );
		/*for( int i = 0; i < 5; i++ )
		{
			addPicture( "picture " + i, new Image( Window.getInstance().getDisplay(), "ressources/tux-gendarme-mobile.png"  ), "pic" + i );
		}*/
		List<String> al = Window.getInstance().mainProcessing.getUserAction().searchAllPictures();
		for( String list : al )
		{
			PictureCom pc = Window.getInstance().mainProcessing.getUserAction().getPicture( list );
			Image img;
			try {
				img = ImageConverter.makeSWTImage( Window.getInstance().getDisplay(), pc.picture.getImage() );
				addPicture( pc.title, img, list );
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		layout();
	}

	private void addButton() {
		Button button = new Button( this, SWT.PUSH );
		button.setText( "Retour aux albums" );
		button.addSelectionListener( new SelectionAdapter() {
			public void widgetSelected( SelectionEvent e ) {
				((PicturesTab)getParent()).setLayoutToAlbums();
			}
		});
	}
	
	private void addLabel( String albumName ) {
		Label label = new Label( this, SWT.BORDER );
		label.setText( albumName );
		GridData gData = new GridData();
		gData.horizontalSpan = 3;
		label.setLayoutData( gData );
	}

	private void clearLayout() {
		Control[] composites = getChildren();
		for( int i = 0; i < composites.length; i++ ) {
			composites[i].dispose();
		}
	}

}
